from django.db import models
from myapp.models import User,UserAddr
from goods.models import Goods
# Create your models here.

# 订单模型类
# 订单id、  varchar(100)，主键
# 用户、 外键
# 收货地址、 外键
# 总价、decimal(9,2)
# 总商品数、int
# 运费、decimal(7, 2)
# 支付方式、char(10)  （1支付宝  2银联）
# 支付状态、char(10)
# 创建时间、 datetime
# 更新时间， datetime
class Order(models.Model):
    PAY_METHOD = (
        (1,'支付宝'),
        (2,'银联')
    )
    PAY_STATUS = (
        (1,'待支付'),
        (2,'待发货'),
        (3,'待收货'),
        (4,'待评价'),
        (5,'待完成'),
    )
    order_id = models.CharField('订单编号',max_length=64,primary_key=True)
    # related_name='orders' 反向查询名称
    user = models.ForeignKey(User,on_delete=models.CASCADE,verbose_name='用户',related_name='orders')
    address = models.ForeignKey(UserAddr,on_delete=models.CASCADE,verbose_name='收货地址')
    total_count = models.IntegerField('商品总数',default=1)
    total_amount = models.DecimalField('总价',max_digits=10,decimal_places=2)
    freight = models.DecimalField('邮费',max_digits=10,decimal_places=2,default=0)
    # SmallIntegerField  小整形
    pay_method = models.SmallIntegerField('支付方式',choices=PAY_METHOD,default=1)
    pay_status = models.SmallIntegerField('支付状态',choices=PAY_STATUS,default=1)
    created_time = models.DateTimeField('创建时间',auto_now_add=True)
    updated_time = models.DateTimeField('更新时间',auto_now=True)

    def __str__(self):
        return '订单：%s'%self.order_id

    class Meta:
        db_table = 'order'
        verbose_name_plural = '订单表'


# 订单商品模型类
class OrderGood(models.Model):
    SCORES = (
        (0,'0'),
        (1,'20'),
        (2,'40'),
        (3,'60'),
        (4,'80'),
        (5,'100'),
    )
    good = models.ForeignKey(Goods,on_delete=models.CASCADE,verbose_name='商品信息')
    count = models.IntegerField('数量',default=1)
    price = models.DecimalField('单价',max_digits=10,decimal_places=2)
    order = models.ForeignKey(Order,on_delete=models.CASCADE,verbose_name='订单')
    comment = models.TextField('评价',default='')
    score = models.SmallIntegerField('评分',choices=SCORES,default=5)
    is_commented = models.BooleanField('是否评价',default=False)
    is_anonymous = models.BooleanField('是否命名',default=False)

    def __str__(self):
        return '订单：%s, 商品:%s'%(self.order_id,self.good.sku_name)


    class Meta:
        db_table = 'order_good'
        verbose_name_plural = '订单商品表'

